package com.example.demo.security;

import com.example.demo.core.ApiResponse;
import com.example.demo.util.ResponseUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.example.demo.constants.ErrorStatusConstants.ACCESS_DENIED_CODE;
import static com.example.demo.constants.ErrorStatusConstants.ACCESS_DENIED_MSG;

/***
 * <p>
 * Description: 权限不足处理
 * </p>
 * @author wangxiaoyuan
 * 2021年12月09日
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        ResponseUtils.writeJson(response, ApiResponse.fail(ACCESS_DENIED_CODE, ACCESS_DENIED_MSG));
    }
}
